/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.modules.system.mapper.*;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.ClazzcourseService;
import me.zhengjie.modules.system.domain.vo.ClazzcourseQueryCriteria;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author shitou
* @date 2024-12-12
**/
@Service
@RequiredArgsConstructor
public class ClazzcourseServiceImpl extends ServiceImpl<ClazzcourseMapper, Clazzcourse> implements ClazzcourseService {

    private final ClazzcourseMapper clazzcourseMapper;

    private final UserMapper userMapper;

    private final StudentMapper studentMapper;

    private final ClazzandstudentMapper clazzandstudentMapper;

    private final CourseMapper courseMapper;

    @Override
    public PageResult<Clazzcourse> queryAll(ClazzcourseQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(clazzcourseMapper.findAll(criteria, page));
    }

    public List<Course> getStudentClazzCourse(ClazzcourseQueryCriteria criteria){

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentUserName = authentication.getName();
        User currentUser = userMapper.findByUsername(currentUserName);

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userId",currentUser.getId());
        Student student = studentMapper.selectOne(queryWrapper);

        QueryWrapper queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("studentId",student.getId());
        Clazzandstudent clazzandstudent = clazzandstudentMapper.selectOne(queryWrapper1);

        List<Course> courseList = new ArrayList<>();
        if(clazzandstudent!=null){
            QueryWrapper queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("clazzId",clazzandstudent.getClazzId());
            List<Clazzcourse> clazzcourseList = clazzcourseMapper.selectList(queryWrapper2);

            List<Integer> courseIds = new ArrayList<>();
            for(Clazzcourse clazzcourse:clazzcourseList){
                courseIds.add(clazzcourse.getCourseid());
            }
            if(courseIds.size()!=0){
                QueryWrapper queryWrapper3 = new QueryWrapper<>();
                queryWrapper3.eq("id",courseIds);
                //获取课程列表
                courseList = courseMapper.selectList(queryWrapper2);
            }

        }
        return courseList;
    }

    @Override
    public List<Clazzcourse> queryAll(ClazzcourseQueryCriteria criteria){
        return clazzcourseMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(Clazzcourse resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Clazzcourse resources) {
        Clazzcourse clazzcourse = getById(resources.getId());
        clazzcourse.copy(resources);
        saveOrUpdate(clazzcourse);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<Clazzcourse> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Clazzcourse clazzcourse : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" clazzid",  clazzcourse.getClazzid());
            map.put(" courseid",  clazzcourse.getCourseid());
            map.put(" teacherid",  clazzcourse.getTeacherid());
            map.put(" lastupdatetime",  clazzcourse.getLastupdatetime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}